package com.dabaozhan.modules.wx_app.facade.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.entity.QueryListBaseEntity;
import com.dabaozhan.modules.wx_app.entity.RiskEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.RiskFacade;
import com.dabaozhan.modules.wx_app.service.RiskService;

@Service("riskFacade")
public class RiskFacadeImpl implements RiskFacade{
	
	@Autowired
	private RiskService riskService;

	@Override
	public PageResult<List<RiskEntity>> getRiskList(QueryListBaseEntity entity) {
		long totalCount = riskService.getRiskCount(entity);
		if(totalCount <=0){
			throw new RRException(ResponseCodeEnum.ResponseCode_400.getCode(),ResponseCodeEnum.ResponseCode_400.getDescription());
		}
		//查询开始区间
		int beginIndex = (entity.getPage()-1)*entity.getSize();
		entity.setBeginIndex(beginIndex);
		//查询结束区间
		int endIndex = entity.getPage()*entity.getSize();
		entity.setEndIndex(endIndex);
		List<RiskEntity> list = riskService.getRiskList(entity);
		PageResult<List<RiskEntity>> pageResult = new PageResult<List<RiskEntity>>();
		pageResult.setListData(list);
		pageResult.setTotal(totalCount);
		return pageResult;
	}

	@Override
	public int addRisk(RiskEntity entity) {
		RiskEntity response = riskService.getRisk(entity);
		if(null != response){
			throw new RRException(ResponseCodeEnum.ResponseCode_108.getCode(),ResponseCodeEnum.ResponseCode_108.getDescription());
		}
		int result = riskService.addRisk(entity);
		if(result <=0){
			throw new RRException(ResponseCodeEnum.ResponseCode_102.getCode(),ResponseCodeEnum.ResponseCode_102.getDescription());
		}
		return result;
	}

	@Override
	public int deleteRisk(RiskEntity entity) {
		int result = riskService.deleteRisk(entity);
		if(result <=0){
			throw new RRException(ResponseCodeEnum.ResponseCode_106.getCode(),ResponseCodeEnum.ResponseCode_106.getDescription());
		}
		return result;
	}

}
